# Simple content providers for Cynin - No viewlets
# registered in configure.zcml and accessed from zpt templates
from zope.interface import Interface
from zope.interface import implements
from zope.component import adapts
from zope.contentprovider.interfaces import IContentProvider
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope.app.pagetemplate import ViewPageTemplateFile
from Acquisition import aq_inner
from Products.CMFCore.utils import getToolByName
from Products.LinguaPlone.interfaces import ITranslatable
from plone.app.layout.navigation.navtree import buildFolderTree

class APKNColophon(object):
	'''
	Customized APKN Colophon to include both iParliaments and
	Cynin branding.
	'''
	implements(IContentProvider)
	adapts(Interface, IDefaultBrowserLayer)
	
	def __init__(self, context, request, view):
		self.context = context
		self.request = request
		self.__parent = view
		
	def update(self):
		pass
	
	render = ViewPageTemplateFile("templates/apkncolophon.pt")

class TranslationsForObject(object):
	'''
	Get Translation Data for Items
	'''
	implements(IContentProvider)
	adapts(Interface, IDefaultBrowserLayer)
	
	def __init__(self, context, request, view):
		self.context = context
		self.request = request
		self.__parent__ = view
		
	def update(self):
		self.langs = self.getTranslationsForItem()
	
	def getTranslationsForItem(self):
		'''
		Return a list of translations and languages with no
		translations {'translations':list of dicts, 
		notranslations:list}
		'''
		translations = {'translations':[], 
		'untranslated': [],
		'lang':'auto',
		'all_langs':[]}
		obj = aq_inner(self.context)
		if ITranslatable.providedBy(obj):
			translations['lang'] = obj.getLanguage()
			lang_tool = getToolByName(self.context, u'portal_languages')
			if lang_tool:
				lang_codes = lang_tool.supported_langs
				translated_langs = lang_codes
				translations['all_langs'] = lang_codes
				for code in lang_codes:
					translation = obj.getTranslation(code)
					if translation:
						translated_langs.remove(code)
						translations['translations'].append(translation)
					else:
						translations['untranslated'].append(code)
			translations['all_langs'] = ','.join(translations['all_langs'])
			translations['untranslated'] = ','.join(translations['untranslated'])
		return translations
	
	#render method of our content provider
	render = ViewPageTemplateFile('templates/lang.pt')


class SlideoutNavigation(object):
	'''
	Render a Tree navigation for APKN
	'''
	implements(IContentProvider)
	adapts(Interface, IDefaultBrowserLayer)
	
	def __init__(self, context, request, view):
		self.context = context
		self.request = request
		self.__parent__ = view
	
	def update(self):
		self.folder_tree = self.getSpacesNavigationTree()
	
	def getSpacesNavigationTree(self):
		ptool = getToolByName(self.context, 'portal_url')
		portal = ptool.getPortalObject()
		base_space = getattr(portal, 'home')
		query = {}
		query['path'] = '/'.join(base_space.getPhysicalPath())
		query['Type'] = 'Space'
		query['sort_on'] = 'modified'
		query['sort_order'] = 'reverse'
		return buildFolderTree(self.context, None, query)
	
	render = ViewPageTemplateFile("templates/slidenav.pt")
